<?

class controller {
    protected $request;
    protected $templatePath;
    protected $title;
    protected $description;
    protected $keywords;
    protected $styles;
    protected $groupID;
    
    function __construct($a_request) {
        $className = get_class($this);
        if (!isset($_SESSION[$className])) $_SESSION[$className] = $this->initSession();
        $this->request = $a_request;
        $this->groupID = '50829404668032';
    }
    
    function redirect($controller, $task='display') {
        $controllerName = $controller.'Controller';
        require_once CONTROLLERS_PATH.$controllerName.'.php';
        $controller = new $controllerName($this->request);
        $controller->templatePath = TEMPLATES_PATH.$task.'.html';
        $controller->$task();
        return $controller;
    }
    
    function getTitle($default) {
        return $this->title?$this->title:$default;
    }
    
    function getDescription($default) {
        return $this->description?$this->description:$default;
    }
    
    function getKeywords($default) {
        return $this->keywords?$this->keywords:$default;
    }
    
    function getStyles($default) {
        return $this->styles?$this->styles:$default;
    }
    
    public function display() {
        require($this->templatePath);
    } 
    
    protected function initSession() {
        return array();
    }
    
    public function getSession($varName) {
        global $_SESSION;
        $className = get_class($this);
        if (!isset($_SESSION[$className][$varName])) return false;
        else return $_SESSION[$className][$varName];
    }
    
    public function setSession($varName, $value) {
        global $_SESSION;
        $className = get_class($this);
        $_SESSION[$className][$varName] = $value;
    }
    
    public function svar($varName, $default=0) {
        global $_SESSION;
        $className = get_class($this);
        $_SESSION[$className][$varName] = $this->request->getVar($varName, 
                                            isset($_SESSION[$className][$varName])?$_SESSION[$className][$varName]:$default);
        return $_SESSION[$className][$varName];
    }
    
    public function getVar($varName, $default=0) {
        return $this->request->getVar($varName, $default);
    }
    
    public function html_uidInput($defaultUid = 0) {
        global $_SESSION;
        if (!isset($_SESSION['uids'])) $_SESSION['uids'] = array();
        $uid = $this->svar('uid', $defaultUid);
        $_SESSION['uids'][$uid] = $uid;
        $uids = '<option value="0">---</option>\n';
        foreach ($_SESSION['uids'] as $uid) {
           $uids .= "<option value=\"{$uid}\">{$uid}</option>\n"; 
        }
        return '<input name="uid" value="'.$uid.'" size="20" id="uid">
                <select name="uids" style="width:200px" onchange="document.getElementById(\'uid\').value = this.value;">
                    '.$uids.'
                </select>';
    }    
}

?>